package com.example.a28796.beautiful;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.example.a28796.beautiful.styAdapter.SearchAdapter;
import com.example.a28796.beautiful.stybean.FuliBean;
import com.example.a28796.beautiful.stybean.HotBean;
import com.example.a28796.beautiful.stybean.SearchBean;
import com.example.a28796.beautiful.stynet.NetConfig;
import com.google.gson.Gson;

import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.x;

import java.util.ArrayList;
import java.util.List;

public class SearchActivity extends AppCompatActivity {
    private RecyclerView mRecyclerView;
    private RecyclerView.Adapter mAdapter;
    private List<SearchBean.ObjBean> mData;
    private GridLayoutManager layoutManager;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search);
        initView();
        initData();
        setData();
    }

    private void setData() {
        layoutManager = new GridLayoutManager(this,3, LinearLayoutManager.HORIZONTAL,false);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setAdapter(mAdapter);
    }

    private void initData() {
        mData = new ArrayList<>();
        mAdapter = new SearchAdapter(this,mData);
        x.http().get(new RequestParams(NetConfig.SEARCH_URL), new Callback.CommonCallback<String>() {
            @Override
            public void onSuccess(String result) {
                SearchBean searchBean = new Gson().fromJson(result, SearchBean.class);
                mData.addAll(searchBean.getObj());
                mAdapter.notifyDataSetChanged();
            }

            @Override
            public void onError(Throwable ex, boolean isOnCallback) {

            }

            @Override
            public void onCancelled(CancelledException cex) {

            }

            @Override
            public void onFinished() {

            }
        });
    }

    private void initView() {
        mRecyclerView = (RecyclerView) findViewById(R.id.rv_search_show);
    }
}
